//
//  LEMDLib.m
//  CreditAddressBook
//
//  Created by LE on 15/12/21.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "LEMDLib.h"
#import "MDDatePickerDialog.h"
#import "MDToast.h"
#import "MDTabBarViewController.h"

static NSString *_dateFormatter;
static LEDatePicker _datePickerBlock;

static MDTabBarViewController *_tabBarViewController;
static NSArray *_tabBarControllers;

@interface LEMDLib ()<MDDatePickerDialogDelegate, MDTabBarViewControllerDelegate>

@end

@implementation LEMDLib

+ (void)showDatePickerWithDateFormatter:(NSString *)formatter completion:(LEDatePicker)completion {
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.year = 1980;
    dateComponents.month = 1;
    dateComponents.day = 5;
//    NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:dateComponents];
    NSDate *date = [NSDate date];
    
    MDDatePickerDialog *datePicker = [[MDDatePickerDialog alloc] init];
    datePicker.minimumDate = date;
    datePicker.selectedDate = date;
    datePicker.delegate = [self self];
    [datePicker setTitleOk:@"确定" andTitleCancel:@"取消"];
    [datePicker show];
    
    _dateFormatter = formatter;
    _datePickerBlock = [completion copy];
}

+ (void)showLongToaskWithMessgage:(NSString *)message {
    MDToast *t = [[MDToast alloc] initWithText:message duration:kMDToastDurationShort];
    [t show];
}

+ (void)tabBarWithController:(UIViewController *)controller subControllers:(NSArray *)controllers subControllerNames:(NSArray *)names {
    MDTabBarViewController *tabBarViewController = [[MDTabBarViewController alloc] initWithDelegate:[self self]];
    _tabBarViewController = tabBarViewController;
    _tabBarControllers = controllers;
    
    tabBarViewController.tabBar.textColor = CYNaviThemeColor;
    tabBarViewController.tabBar.normalTextColor = [UIColor grayColor];
    tabBarViewController.tabBar.backgroundColor = [UIColor whiteColor];
    tabBarViewController.tabBar.indicatorColor = CYNaviThemeColor;
    tabBarViewController.tabBar.normalTextFont = [UIFont systemFontOfSize:12];
    //    tabBarViewController.tabBar.rippleColor = CYNaviThemeColor;
    
    [tabBarViewController setItems:names];
    
    UIView *controllerView = tabBarViewController.view;
    [controller addChildViewController:tabBarViewController];
    [controller.view addSubview:controllerView];
    [tabBarViewController didMoveToParentViewController:controller];
    id<UILayoutSupport> rootTopLayoutGuide = controller.topLayoutGuide;
    id<UILayoutSupport> rootBottomLayoutGuide = controller.bottomLayoutGuide;
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(rootTopLayoutGuide, rootBottomLayoutGuide, controllerView);
    
    [controller.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[rootTopLayoutGuide]-50-["@"controllerView]-0-["@"rootBottomLayoutGuide]" options:0 metrics:nil views:viewsDictionary]];
    [controller.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[controllerView]|" options:0 metrics:nil views:viewsDictionary]];
//    [controllerView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
}

#pragma mark - MDDatePickerDialogDelegate
+ (void)datePickerDialogDidSelectDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:_dateFormatter];
    dateFormatter.timeZone = [NSTimeZone localTimeZone];//设置时区
    NSString *dateStr = [dateFormatter stringFromDate:date];
    _datePickerBlock(dateStr, date);
    _datePickerBlock = nil;
}
- (void)datePickerDialogDidSelectDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:_dateFormatter];
    dateFormatter.timeZone = [NSTimeZone localTimeZone];//设置时区
    NSString *dateStr = [dateFormatter stringFromDate:date];
    _datePickerBlock(dateStr, date);
    _datePickerBlock = nil;
}

#pragma mark - delegate
+ (UIViewController *)tabBarViewController: (MDTabBarViewController *)viewController viewControllerAtIndex:(NSUInteger)index {
    UIViewController *controller = _tabBarControllers[index];
    return controller;
}
- (UIViewController *)tabBarViewController: (MDTabBarViewController *)viewController viewControllerAtIndex:(NSUInteger)index {
    return nil;
}
@end
